package work.work60;
/**
 * 超级数组
 */
public class SuperArray {
    private Object [] objects = new Object[0];
    /**
     * 增加元素到数组中
     * @param value
     */
    public void add(Object value){
        Object [] newobjects = new Object[objects.length + 1];
        for (int i = 0; i < objects.length; i++){
            newobjects[i] = objects[i];
        }
        newobjects[newobjects.length - 1] = value;
        //把新数组的地址赋给旧数组
        objects = newobjects;
    }
    /**
     *删除指定下标的元素
     */
    public void remove(int index){
        Object [] newobjects = new Object[objects.length - 1];
        for (int i = 0; i < newobjects.length; i++){
            if (i < index){
                newobjects[i] = objects[i];
            }else {
                newobjects[i] = objects[i + 1];
            }
        }
        //把新数组的地址赋给旧数组
        objects = newobjects;
    }
    /**
     * 根据下标获取指定元素
     */
    public Object get(int index){
        return objects[index];
    }
    /**
     * 获取数组长度
     */
    public int size(){
        return objects.length;
    }

    public static void main(String[] args) {
        SuperArray ary = new SuperArray();
        ary.add("小哥哥");
        ary.add("小姐姐");
        ary.add(100);
        ary.add(10.1);

        System.out.println("长度：");
        System.out.println(ary.size());
        System.out.println("下标为2的元素：");
        System.out.println(ary.get(2));
        System.out.println("数组的元素：");
        for (int i = 0; i < ary.size(); i++){
            System.out.print(ary.get(i) + ",");
        }
        System.out.println();
        System.out.println("删除下标为2的数组");
        ary.remove(2);
        for (int i = 0; i < ary.size(); i++){
            System.out.print(ary.get(i) + ",");
        }
    }
}
